<template>
    <div class="container">
        <div class="am-cf am-padding am-padding-bottom-0">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">新增友情链接</strong> /
                <small>Add Link</small>
            </div>
        </div>

        <hr>

        <template>
            <el-row :gutter="20">
                <el-col :span="13">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="链接名称" prop="name">
                            <el-input v-model="ruleForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="链接地址" prop="address">
                            <el-input v-model="ruleForm.address"></el-input>
                        </el-form-item>
                        <el-form-item label="链接描述" prop="description">
                            <el-input type="textarea" v-model="ruleForm.description"></el-input>
                        </el-form-item>
                        <el-form-item label="链接排序" prop="sort_order">
                            <el-input v-model.number="ruleForm.sort_order"></el-input>
                        </el-form-item>
                        <el-form-item label="是否显示" prop="delivery">
                            <template>
                                <el-radio v-model="ruleForm.is_show" label="1">是</el-radio>
                                <el-radio v-model="ruleForm.is_show" label="0">否</el-radio>
                            </template>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">新增</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </template>

    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    name: '',
                    address: '',
                    description: '',
                    sort_order: '',
                },
                rules: {
                    name: [
                        {required: true, message: '请输入链接名称', trigger: 'blur'},
                        {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'change'}
                    ],
                    address: [
                        {required: true, message: '请输入链接地址', trigger: 'blur'},
                        {type: 'url', message: '请输入正确链接地址', trigger: 'change'}
                    ],
                    description: [
                        {required: true, message: '请输入链接描述', trigger: 'blur'}
                    ],
                    sort_order: [
                        {type: 'number', message: '请输入数字', trigger: 'change'}
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('http://mishop.test/admin/crm/links', this.ruleForm)
                            .then((response) => {
                                this.$router.push({name: 'index'})
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>
